package com.ist.viewpagerhelper.indicator;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ist.viewpagerhelper.R;
import com.ist.viewpagerhelper.bean.ViewPagerBean;


/**
 * Created by zhengshaorui on 2017/10/29.
 * csdn: http://blog.csdn.net/u011418943
 */
public class TransIndicator extends LinearLayout implements ViewPager.OnPageChangeListener {
    private static final String TAG = "zsr";


    /**
     * ui
     */
    private View mStartView;
    private View mFirstView;
    private View mSecondView;

    /**
     * normal and logic
     */
    private Context mContext;
    private int mCount;
    private Path mPath;
    private Paint mPaint;
    private int mCircleRadius = 8;
    private int mMoveDistance = 0;
    private int[] mFirstPosition = new int[2];
    private int[] mSecondPositon = new int[2];
    private int mMoveSize;
    private int mLeftMargin;
    private int mDefaultcolor;
    private int mMovecolor;


    public TransIndicator(Context context) {
        this(context,null);
    }

    public TransIndicator(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TransIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TransIndicator);
        mLeftMargin = (int) ta.getDimension(R.styleable.TransIndicator_trans_leftmargin,20);
        mCircleRadius = (int) ta.getDimension(R.styleable.TransIndicator_trans_size,8);
        mDefaultcolor = ta.getColor(R.styleable.TransIndicator_trans_defaultcolor,
                mContext.getResources().getColor(R.color.gray_cccc));
        mMovecolor = ta.getColor(R.styleable.TransIndicator_trans_movecolor,
                mContext.getResources().getColor(R.color.white));
        ta.recycle();



        mPath = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mMovecolor);
        setClipChildren(false);
        setClipToPadding(false);

    }

    public void setViewPagerData(ViewPagerBean bean){
        if (bean.viewPager != null){
            mCount = bean.count;
            ViewPager viewPager = (ViewPager) bean.viewPager;
            if (mCount == 0){
                return;
            }
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(mLeftMargin,0,0,0);
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.OVAL);
            drawable.setSize(mCircleRadius*2,mCircleRadius*2);
            drawable.setColor(mDefaultcolor);

            for (int i = 0; i < mCount; i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setBackground(drawable);
                imageView.setLayoutParams(params);
                if (i == 0){

                    mFirstView = imageView;
                }
                if (i == 1){
                    mSecondView = imageView;
                }

                addView(imageView);
            }

            viewPager.addOnPageChangeListener(this);
        }
    }

    public void setViewPager(ViewPager viewPager,View startView){
        mStartView = startView;
        if (viewPager != null){
            mCount = viewPager.getAdapter().getCount();
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(mLeftMargin,0,0,0);
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.OVAL);
            drawable.setSize(mCircleRadius*2,mCircleRadius*2);
            drawable.setColor(mDefaultcolor);

            for (int i = 0; i < mCount; i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setBackground(drawable);
                imageView.setLayoutParams(params);
                if (i == 0){

                    mFirstView = imageView;
                }
                if (i == 1){
                    mSecondView = imageView;
                }

                addView(imageView);
            }
            viewPager.addOnPageChangeListener(this);
        }
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mMoveDistance,0);
        canvas.drawPath(mPath,mPaint);
        canvas.restore();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (position % mCount == 3 && positionOffset > 0){
            mMoveDistance = 0;
        }else {
            mMoveDistance = (int) (positionOffset * mMoveSize + position % mCount * mMoveSize);
        }
        invalidate();
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (mFirstView == null){
            return;
        }
        if (mSecondView == null){
            return;
        }
        //第一个控件在屏幕的位置
        mFirstView.getLocationOnScreen(mFirstPosition);
        mSecondView.getLocationOnScreen(mSecondPositon);
        // 两个圆点之间要移动的距离
        mMoveSize = mSecondPositon[0] - mFirstPosition[0];
        //获取liearlayout 的位置，两个相减才是正确的位置
        int[] pos = new int[2];
        getLocationInWindow(pos);

        // 获取到第一个小圆点的坐标，并重绘
        mPath.addCircle(mFirstPosition[0] - pos[0] + mCircleRadius,getHeight()/2,mCircleRadius, Path.Direction.CW);

    }


}
